package com.example.statistics

import android.app.Application
import android.util.Log
import com.example.statistics.bean.UserBean
import com.example.statistics.constant.PreferenceConstants
import com.example.statistics.ex.preferenceFind
import com.example.statistics.ex.preferenceFindAll
import com.example.statistics.net.UrlConfig
import com.example.statistics.net.callback.DownloadDialogHttpCallback
import com.example.statistics.utils.FileUtils
import com.jhj.httplibrary.HttpCall
import com.jhj.httplibrary.callback.DownloadHttpCallback
import com.jhj.httplibrary.model.HttpParams
import com.jhj.imageselector.ImageSelector
import com.jhj.imageselector.config.ImageConfig
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.QbSdk.PreInitCallback
import kotlinx.android.synthetic.main.activity_file_display.*
import org.jetbrains.anko.toast
import java.io.File


class MyApplication : Application() {

    companion object {
        var instance: MyApplication = MyApplication()
        var isTBSLoadingSuccess = false
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        ImageConfig.getInstance().compressSize = 1024
        val user = preferenceFind(UserBean::class.java, PreferenceConstants.USER)
        HttpCall.init(this)
                .setBaseUrl(UrlConfig.BASE_URL)
                .setParams(HttpParams("userId" to user?.ID))
        // .setBaseUrl("http://192,168.1.1:8081")

        // 在调用TBS初始化、创建WebView之前进行如下配置
        val map = HashMap<String, Any>()
        map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
        map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
        QbSdk.initTbsSettings(map)
        val url = UrlConfig.BASE_URL + "/upload/core/x5.tbs32.apk"
        val folder: String? = FileUtils.getSDPath("core")
        val path = "$folder/x5.tbs32.apk"


        val isSuccess = QbSdk.canLoadX5(this)
        if (File(path).exists() && !isSuccess) {
            QbSdk.installLocalTbsCore(this, 46140, path)
            Log.e("xx", 46140.toString())
        }
        QbSdk.initX5Environment(this, object : PreInitCallback {
            override fun onCoreInitFinished() {
            }

            override fun onViewInitFinished(p0: Boolean) {
                Log.e("xxx",p0.toString())
            }

        })
        Log.e("xx", isSuccess.toString())
        Log.e("xx", QbSdk.getTbsVersion(this).toString())
        //QbSdk.setDownloadWithoutWifi(true);


    }

}